// pages/login.js
import {
  LoginModel
} from '../../models/login.js'
let loginModel = new LoginModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    password: '',
    count: Number,
    showCode: true,
    code: '',
    iv: '',
    encryptedData: '',

  },
  //用户名
  userNameInput(e) {
    this.setData({
      userName: e.detail.value
    })
  },
  //验证码
  passwordInput(e) {
    this.setData({
      password: e.detail.value
    })
  },
  //点击游客按钮
  visitorBtnClick() {
    wx.navigateTo({
      url: '/pages/visitorList/visitor',
    })
  },
  //点击登录按钮
  loginBtnClick() {
    wx.reLaunch({
      url: '/pages/hdd-changeView/hdd-changeView',
    })
    return
    if (!this.data.userName) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
      })
      return
    }
    if (!this.data.password) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none',
      })
      return
    }
    const data = loginModel.login({
      loginName: this.data.userName,
      password: this.data.password
    })
    data.then(res => {
      wx.setStorageSync('token', res.data.data.token)
      if (res.data.success) {
        wx.reLaunch({
          url: '/pages/hhd-home/hhd-home',
        })
      } else {
        if (res.data.data.msg === 'code:NOT_EXIT_USER_INFO' || res.data.data.msg === 'code:NOT_EXIT_EMP_INFO') {
          wx.navigateTo({
            url: `/pages/register/register?mobile=${this.data.userName}`,
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.data.msg,
          })
        }
      }
    })
  },

  pushToHddSystem() {
    wx.reLaunch({
      url: '/pages/hdd-login/hdd-login',
    })
  },

  getPhoneNumber(e) {
    wx.login({
      success: res => {
        let encryptedData = e.detail.encryptedData
        let iv = e.detail.iv
        let code = res.code
        loginModel.getSessionKey({
          code: code
        }).then(res => {
          console.log(res)
          // loginModel.autoLogin({
          //   mobile: data.phoneNumber
          // }).then(res => {
          //   wx.setStorageSync('token', res.data.data.token)
          //   if (res.data.success) {
          //     wx.reLaunch({
          //       url: '/pages/hhd-home/hhd-home',
          //     })
          //   } else {
          //     if (res.data.data.msg === 'code:NOT_EXIT_USER_INFO' || res.data.data.msg === 'code:NOT_EXIT_EMP_INFO') {
          //       wx.navigateTo({
          //         url: `/pages/register/register?mobile=${this.data.userName}`,
          //       })
          //     } else {
          //       wx.showModal({
          //         title: '提示',
          //         content: res.data.data.msg,
          //       })
          //     }
          //   }
          // })
        })
      }
    })
  },
  //获取验证码
  getCode() {
    if (!this.data.userName) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
      })
      return
    }
    const TIME_COUNT = 60
    if (this.data.showCode) {
      loginModel.getCode(this.data.userName)
      this.setData({
        showCode: false,
        count: TIME_COUNT,
      })
      var timer = setInterval(() => {
        if (this.data.count > 0 && this.data.count <= TIME_COUNT) {
          let temp = (this.data.count - 1)
          this.setData({
            count: temp
          })
        } else {
          this.setData({
            showCode: true
          })
          clearInterval(timer)
        }
      }, 1000)
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: '登录',
    })
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#3B80FE',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})